﻿using KAKA.Domain;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Morus;
using KAKA.Repository;
using System.Web;

namespace KAKA
{
    public static class WebUtil
    {
        public static string GetLargePortrait(Member member)
        {
            string path = "Member/{0}/{1}{2}";
            string folder = member.Portrait.IsEmpty() ? "default" : member.Id.Shrink();
            string portrait = member.Portrait;
            string size = "-large";
            if (member.Portrait.IsEmpty())
            {
                if (member.Gender == Gender.Female)
                {
                    portrait = "girl";
                }
                else
                {
                    portrait = "boy";
                }
                size = string.Empty;
            }
            return path.FormatWith(folder, portrait, size);
        }

        public static SelectList GetRange(int min, int max, int selected, bool includeEmpty, string emptyComment)
        {
            var result = new List<SelectListItem>(max - min);
            while (min <= max)
            {
                result.Add(new SelectListItem
                {
                    Selected = min == selected,
                    Text = min.ToString(),
                    Value = (min++).ToString()
                });
            }
            if (includeEmpty)
            {
                result.Insert(0, new SelectListItem()
                {
                    Text = "-- {0} --".FormatWith(emptyComment ?? "请选择"),
                    Value = string.Empty
                });
            }
            return new SelectList(result, "Value", "Text", selected);
        }

        public static SelectList ToSelectList(this IEnumerable<Dictionary> source)
        {
            return ToSelectList(source, false, null);
        }

        public static SelectList ToSelectList(this IEnumerable<Dictionary> source, bool includeEmpty)
        {
            return ToSelectList(source, includeEmpty, null);
        }

        public static SelectList ToSelectList(this IEnumerable<Dictionary> source, bool includeEmpty, string emptyComment)
        {
            var result = source.ToList().Select(r => new SelectListItem()
            {
                Text = r.Name,
                Value = r.Value,
            }).ToList();

            if (includeEmpty)
            {
                result.Insert(0, new SelectListItem()
                {
                    Text = "-- {0} --".FormatWith(emptyComment ?? "请选择"),
                    Value = string.Empty
                });
            }

            return new SelectList(result, "Value", "Text");
        }

        public static string GetSelectListText(SelectList select, string value)
        {
            var item = select.FirstOrDefault(r => r.Value == value);
            if (item != null && item.Value.IsNotEmpty())
            {
                return item.Text;
            }
            return string.Empty;
        }

        public static User GetCurrentUser()
        {
            User user = HttpContext.Current.Items[typeof(User)] as User;

            if (user == null)
            {
                user = IoC.Resolve<IUserRepository>().Get(HttpContext.Current.User.Identity.Name);

                if (user != null)
                {
                    HttpContext.Current.Items[typeof(User)] = user;
                }
            }

            return user;
        }

        public static string GetMaskEmail(string email)
        {
            var atIndex = email.IndexOf('@');
            var suffix = email.Substring(atIndex, email.Length - atIndex);
            if (email.Length > 2)
            {
                var prefix = email.Substring(0, 2);
                var mask = email.Substring(2, atIndex - 2);
                return string.Concat(prefix, new string('*', mask.Length), suffix);
            }
            else
            {
                return string.Concat(email.Substring(0, 1), "*", suffix);
            }
        }
    }
}